﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Upreader.Application.Model;
using System.Threading;

namespace Upreader.Infrastructure.ProtoBuf
{
    /// <summary>
    /// Internal in-memory synchronized shared representation of the domain context
    /// </summary>
    public class DataContext
    {
        #region Fields

        private readonly ISet<Server> _servers;
        private readonly ISet<Download> _downloads;
        private readonly ISet<File> _files;
        private readonly ISet<Segment> _segments;

        private readonly ReaderWriterLockSlim _synchronizationContext;

        private int _nextServerId;
        private int _nextDownloadId;
        private int _nextFileId;
        private int _nextSegmentId;

        #endregion // Fields

        #region Constructor

        public DataContext()
        {
            _servers = new HashSet<Server>();
            _downloads = new HashSet<Download>();
            _files = new HashSet<File>();
            _segments = new HashSet<Segment>();

            _synchronizationContext = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);

            _nextServerId = 1;
            _nextDownloadId = 1;
            _nextFileId = 1;
            _nextSegmentId = 1;
        }

        #endregion // Constructor

        #region Properties

        public ISet<Server> Servers { get { return _servers; } }

        public ISet<Download> Downloads { get { return _downloads; } }

        public ISet<File> Files { get { return _files; } }

        public ISet<Segment> Segments { get { return _segments; } }

        public ReaderWriterLockSlim SynchronizationContext { get { return _synchronizationContext; } }

        public int NextServerId 
        { 
            get { return _nextServerId; }
            set { _nextServerId = value; }
        }

        public int NextDownloadId
        { 
            get { return _nextDownloadId; }
            set { _nextDownloadId = value; }
        }

        public int NextFileId 
        { 
            get { return _nextFileId; }
            set { _nextFileId = value; }
        }

        public int NextSegmentId 
        { 
            get { return _nextSegmentId; }
            set { _nextSegmentId = value; }
        }

        #endregion // Properties      
    }
}
